package ios.di.logviewer.ui;



import ios.di.logviewer.data.LogEntry;
import ios.di.logviewer.data.LogEntryFilter;
import ios.di.logviewer.data.LogFileLoader;
import ios.di.logviewer.data.PageProcessor;
import ios.di.logviewer.util.DateUtil;
import ios.di.logviewer.xml.parser.FileEndException;
import ios.di.logviewer.xml.parser.LogEntriesLoader2;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.filechooser.FileFilter;





/***
 * 程序的主界面
 * @author LuoHouQi
 *
 */

public class MainFrame extends JFrame implements ActionListener {
	private String filePath;
	DataTableModel tm;
	LogEntriesLoader2 logLoader;
	LogFileLoader fileLoader;
	LogEntryFilter logFilter;
	SearchPanel searchPanel;
	PagingPanel pagePanel;
	JTable table;
	private void openActionPerformed(ActionEvent e)
	{
		File file = new File(".");
		javax.swing.JFileChooser chooser = new JFileChooser(file);
		chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
		
		int ret = chooser.showOpenDialog(this);
		if (ret == JFileChooser.APPROVE_OPTION)
			this.filePath = chooser.getSelectedFile().getPath();
	}
	
	private void searchActionPerformed(ActionEvent e)
	{
		
		if(filePath!=null)
		{
			String title = "输入出错";
			
			String businessType= (String) this.searchPanel.getBusinessType_CB().getSelectedItem();
			String vendorID = this.searchPanel.getVendorID_TA().getText().trim();
			Date businessStartDate = null;
			try {
				businessStartDate = DateUtil.getDateUtil(DateUtil.PATTERN_1).parseDate(this.searchPanel.getBusinessStartDate_TA().getText());
			} catch (ParseException e1) {
				
				String message = this.searchPanel.getBusinessStartDate_TA().getText()+"不是一个合法的日期，请输入合法的业务起始日期";				
				new ErrorMessage(this,message,title).show();
				return;
			}
			Date businessEndDate = null;
			try {
				businessEndDate = DateUtil.getDateUtil(DateUtil.PATTERN_1).parseDate(this.searchPanel.getBusinessEndDate_TA().getText());
			} catch (ParseException e1) {
				
				String message = this.searchPanel.getBusinessEndDate_TA().getText()+"不是一个合法的日期，请输入合法的业务截止日期";	
				new ErrorMessage(this,message,title).show();
				return;
			}
			
			Date happenStartDate = null;
			try {
				happenStartDate = DateUtil.getDateUtil(DateUtil.PATTERN_1).parseDate(this.searchPanel.getHappenStartDate_TA().getText());
			} catch (ParseException e1) {
				String msg = this.searchPanel.getHappenStartDate_TA().getText()+"不是一个合法的日期，请输入合法的日志起始日期";
				new ErrorMessage(this,msg,title).show();
				return;
			}
			
			Date happenEndDate = null;
			try {
				happenEndDate = DateUtil.getDateUtil(DateUtil.PATTERN_1).parseDate(this.searchPanel.getHappenEndDate_TA().getText());
			} catch (ParseException e1) {
				String msg = this.searchPanel.getHappenEndDate_TA().getText()+"不是一个合法的日期，请输入合法的日志截止日期";
				new ErrorMessage(this,msg,title).show();
				return;
			}
			
			this.logFilter = new LogEntryFilter();
			this.logFilter.setBussnessType(businessType);
			this.logFilter.setVendorID(vendorID);
			this.logFilter.setBunessDateStart(businessStartDate);
			this.logFilter.setBunessDateEnd(businessEndDate);
			this.logFilter.setHappenDateStart(happenStartDate);
			this.logFilter.setHappenDateend(happenEndDate);
			
			this.fileLoader = new LogFileLoader(this.filePath,happenStartDate,happenEndDate);
			
				this.logLoader = new LogEntriesLoader2(this.logFilter,fileLoader);
		
				this.pagePanel.setPagePro(this.logLoader);
				List<LogEntry> logList;
				try {
					logList = this.logLoader.next();
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					logList = new ArrayList();
				} catch (UnsupportedEncodingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					logList = new ArrayList();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					logList = new ArrayList();
				}
				this.tm.setLogs(logList);
			
			this.fireUpdate();
			
		}
		else
		{
			String title = "请选择日志文件目录";
			String message = "请先从 文件->打开 中选择日志文件目录";			
			new ErrorMessage(this,message,title).show();
			return;
			
		}
	}
	private void nextActionPerformed(ActionEvent e)
	{
		List<LogEntry> logList;
		try {
			logList = this.logLoader.next();
			
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			logList = new ArrayList();
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			logList = new ArrayList();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			logList = new ArrayList();
		}
		this.tm.setLogs(logList);
		this.fireUpdate();
	}
	
	private void previousActionPerformed(ActionEvent e)
	{
		List<LogEntry> logList;
		try {
			logList = this.logLoader.previous();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			logList = new ArrayList();
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			logList = new ArrayList();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			logList = new ArrayList();
		} catch (FileEndException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			logList = new ArrayList();
		}
		this.tm.setLogs(logList);
		this.fireUpdate();
	}
	
	private void quitActionPerformed(ActionEvent e)
	{
		this.dispose();
		System.exit(0);
	}
	@Override
	public void actionPerformed(ActionEvent e)  {
		String eventName = e.getActionCommand();

		if (eventName.equals("Open")) {
			this.openActionPerformed(e);
		} else if (eventName.equals("Search")) {
			this.searchActionPerformed(e);
		} else if (eventName.equals("NextPage")) {
			this.nextActionPerformed(e);
		} else if (eventName.equals("PrePage")) {
			this.previousActionPerformed(e);
		}else if (eventName.equals("Quit")) {
			this.quitActionPerformed(e);
		}

	}

	/**
	 * @param args
	 */

	public MainFrame() {
		
		 logLoader =null;
		
		this.initialize();
	}

	private JMenuBar createMenubar() {
		JMenuBar menuBar = new JMenuBar();

		//File Menu
		JMenu fileMenu = new JMenu("文件");
		menuBar.add(fileMenu);
		
		JMenuItem openMenuItem = new JMenuItem("打开");
		openMenuItem.setActionCommand("Open");
		openMenuItem.addActionListener(this);
		fileMenu.add(openMenuItem);
		
		JMenuItem quitMenuItem = new JMenuItem("退出");
		quitMenuItem.setActionCommand("Quit");
		quitMenuItem.addActionListener(this);
		fileMenu.add(quitMenuItem);

		return menuBar;
	}

	private void initialize() {
		setTitle("Log Viewer");

		// Create Menu
		JMenuBar menuBar = this.createMenubar();
		setJMenuBar(menuBar);
		BorderLayout layout =  new BorderLayout();
		this.setLayout(layout);

		searchPanel = new SearchPanel(this);
	//	searchPanel.setSize(800, 200);
		this.add(searchPanel, BorderLayout.NORTH);
		layout.maximumLayoutSize(searchPanel);
		
		tm = new DataTableModel();
		this.table = new DataTable(tm);
		this.add(new JScrollPane(this.table), BorderLayout.CENTER);
		
		pagePanel = new PagingPanel(this, logLoader);
		this.add(pagePanel, BorderLayout.SOUTH);
	}
	private void fireUpdate()
	{
		if(this.logLoader!=null)
		{
			this.pagePanel.setPagePro(this.logLoader);
			this.tm.fireTableDataChanged();
			this.pagePanel.fireUpdate();	
		}
	}
	public static void main(String[] args) {
		JFrame frame = new MainFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800, 625);
		frame.setVisible(true);

	}

}
